var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;

var bindEvent = function(){
    $('.sms-btn-submit').click(function(){
        var _user = SA.get('USER');
        var id = $(this).attr('data-id');
        var params = {
            method:'get',
            login : _user.login,
            isReceiveSms : id>0?false:true
        };
        var _this = this;
        api.req('/updateReceiveSmsStatus',params,function(data){
            if(data.isReceiveSms){
                $(_this).val('关闭订阅').attr('data-id',1);
            }else{
                $(_this).val('开通订阅').attr('data-id',0);
            }
        });
    });
}


//演示模块
var Sms = React.createClass({
    mixins: [ItemMixin],
    render:function(){
        var _user = SA.get('USER');
        var isReceiveSms = _user.info.isReceiveSms;  //是否已经订阅
        return (
            <div className="uc-sms">
                <p style={{width:'94%',margin:'10px auto','line-height':'0.3rem','font-size':'0.14rem'}}>根据您所在的小区及时向您{_user.info.mobile}手机号中发送小区公告通知</p>
                <div>
                    <input type="submit" data-id={isReceiveSms?1:0} defaultValue={isReceiveSms?'关闭订阅':'开通订阅'} className="f_btn sms-btn-submit" />
                </div>
            </div>
        );
    }
});

function start(name){
    return pages.new({
        boot:function(){},
        trigger:function(){
            this.libs.changeTitle('短信订阅');    //更改当前页面标题
            this.main();
        },
        main: function(self, data){
            this.render(
                <Sms itemDefaultMethod={bindEvent} data={data}/>,
                document.getElementById(name)
            )
        }
    })
}

module.exports = start;
